package com.tf_cut.backend.sms.service;

import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.sms.pojo.Sms;
import com.tf_cut.backend.sms.vo.SmsVo;
import com.tf_cut.backend.util.SmsUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ISmsServiceImpl extends CommonServiceImpl<SmsVo, Sms,Long> implements ISmsService {

    SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
    @Override
    public String sendSms(String mobile) {
        String code = String.valueOf(Math.round(Math.random()*(9999-1000+1)+1000));
        if(code.length()==3){
            code="0"+code;
        }else if(code.length()==2){
            code="00"+code;
        }else if(code.length()==1){
            code="000"+code;
        }
        SmsUtil.sendSms(new String[]{"+86"+mobile},new String[]{code});
        SmsVo sms = new SmsVo();
        sms.setMobile(mobile);
        sms.setSmsCode(code);
        sms.setSmsType("1");
        String id=sdf.format(new Date())+(int)(Math.random()*1000);
        sms.setId(Long.parseLong(id));
        sms.setCreateTime(new Date());
        sms.setUpdateTime(new Date());
        super.save(sms);
        return code;
    }



}
